package com.hx.controll;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hx.pojo.CosDep;
import com.hx.pojo.CosmeticInfo;
import com.hx.pojo.DepotInfo;
import com.hx.service.CosService;
import com.hx.service.DepService;

@Controller
@RequestMapping("/view")
public class CosMangner {
	@Autowired
	private CosService cs;
	@Autowired
	private DepService ds;
	
	
	@RequestMapping("/{id}")
	public String getCosDep(Model m,@PathVariable("id")Integer pageNow ){
		Page<CosDep> page=ds.getPage(pageNow-1,1);
		int numpage=page.getTotalPages();
		if(pageNow>numpage)pageNow=numpage;
		m.addAttribute("pages", numpage);//总页数
		m.addAttribute("curpage", page.getNumber());//当前页
		m.addAttribute("lc",page.getContent());//list<cosdep>集合
		return "show.jsp";
	}
	
	@RequestMapping("/addCos")
	@ResponseBody
	public Result addCos(CosmeticInfo cos){
		Result rs=new Result();
		cs.save(cos);
		rs.setFlag(true);
		return rs;
	}
	
	@RequestMapping("/addDep")
	public String goDep(Model m){
		m.addAttribute("lc", cs.findAll());
		return "addDep.jsp";
	} 
	
	@RequestMapping("/savedep")
	@ResponseBody
	public Result saveDep(@RequestParam("cosmeticId") Integer cid,DepotInfo dep){
		System.out.println("====start");
		Result rs=new Result();
		CosmeticInfo cos=cs.findByCosmeticId(cid);
		dep.setCosmetic(cos);
		dep.setInTime(new Date());
		ds.save(dep);
		System.out.println("rs=====>"+rs);
		rs.setFlag(true);
		return rs;		
	}
	
	@RequestMapping("/delCosDep")
	@ResponseBody
	public void delCosDep(@RequestParam("id")Integer id,@RequestParam("curpage")Integer pageNow,HttpServletResponse hrs) throws IOException{
	ds.deleteId(id);
	hrs.sendRedirect("../"+pageNow);
		
	}

	
}
